# -*- coding: utf-8 -*-
"""
file:ch3_1二次函数
author: Fuwen
time: 2022/7/16 7:43
software:PyCharm
"""
import matplotlib.pyplot as plt
import numpy as np


# 二次函数 y = x**2
x = np.linspace(-10, 10, 1001)
y = [x**2 for x in x]

plt.axis([-11, 11, -10, 110])
plt.plot(x, y)

# 选取两点直线， 逼近于两点之间的切线
a = [x[780], x[790]]
b = [y[780], y[790]]
ab_slope = (b[1] - b[0]) / (a[1] - a[0])
y1 = b[0]
x1 = a[0]
c = y1 - x1*ab_slope

# 构建a-b切线的函数 f(x) = ab_slope*x + c   # 一次函数 也是原函数在该点的导数，微分
slope_x = np.linspace(-5,10, 101)
slope_y = [ab_slope * x + c for x in slope_x]
plt.plot(slope_x, slope_y, '--')

plt.xticks(range(-11, 11, 1))
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.show()